/**  
 * @Title:  AttachmentsImpl.java   
 * @Package com.gtjy.p2p.modules.tiles.sys.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: 吴元森 
 * @date:   2018年11月29日 下午5:33:09   
 * @version V1.0 
 * @Copyright: 2018 吴元森.
 */
package com.platform.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platform.annotation.Authentication;
import com.platform.base.PageUtil;
import com.platform.base.R;
import com.platform.dao.AttachmentsDAO;
import com.platform.dto.Attachments;
import com.platform.ext.mybatis.annotation.UseDataSource;
import com.platform.service.IAttachments;

import cn.hutool.core.io.FileUtil;

/**
 * @ClassName: AttachmentsImpl
 * @Description:统一附件管理
 * @author: 吴元森
 * @date: 2018年11月29日 下午5:33:09
 * 
 * @Copyright: 2018 吴元森.
 */
@Service
public class AttachmentsImpl implements IAttachments {

	@Autowired
	private AttachmentsDAO<Attachments> attchmentDAO;

	/**
	 * <p>
	 * Title: insert
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param attachment
	 * @return
	 * @see com.gtjy.p2p.modules.tiles.sys.IAttachments#insert(com.gtjy.p2p.modules.sys.dto.Attachments)
	 */
	@Override
	@Authentication()
	@UseDataSource
	public R<?> insert(Attachments attachment) {
		if(attchmentDAO.insert(attachment) > 0) {
			return R.success();
		}else {
			return R.fail();
		}
	}

	/**
	 * <p>
	 * Title: updateBySelective
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param attachment
	 * @return
	 * @see com.gtjy.p2p.modules.tiles.sys.IAttachments#updateBySelective(com.gtjy.p2p.modules.sys.dto.Attachments)
	 */
	@Override
	@Authentication()
	@UseDataSource
	public R<?> updateBySelective(Attachments attachment) {
		if(attchmentDAO.updateBySelective(attachment) > 0) {
			return R.success();
		}else {
			return R.fail();
		}
	}

	/**
	 * <p>
	 * Title: deleteByPrimaryKey
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param qquuid
	 * @return
	 * @see com.gtjy.p2p.modules.tiles.sys.IAttachments#deleteByPrimaryKey(java.lang.Object)
	 */
	@Override
	@Authentication
	@UseDataSource
	public R<?> deleteByPrimaryKey(Object qquuid) {
		Attachments attach = attchmentDAO.selectByPrimaryKey(qquuid);
		FileUtil.del(attach.getFilepath());
		if(attchmentDAO.deleteByPrimaryKey(qquuid) > 0) {
			return R.success();
		}else {
			return R.fail();
		}
	}

	/**
	 * @Title: selectByParam
	 * @Description: 根据主键查询附件信息
	 * @param ids
	 * @return R<?>
	 */
	@Override
	@Authentication()
	@UseDataSource()
	public R<?> selectByParam(String ids) {
		java.util.List<Attachments> atts = new ArrayList<Attachments>();
		for (String qquuid : ids.split(",")) {
			Attachments attach = attchmentDAO.selectByPrimaryKey(qquuid);
			atts.add(attach);
		}
		return R.success(atts);
	}
	
	@Override
	@Authentication()
	@UseDataSource()
	public List<Attachments> selectByMapPaging(PageUtil pageUtil){
		return attchmentDAO.selectByMapPaging(pageUtil);
	}
}
